package com.t2t.top.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.utils.ZKPaths;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AppMonitor {

    private CuratorFramework client;

    /**
     * 初始化zookeeper
     */
    public void initialize() throws Exception {
        String connectionString = Constant.ZK_ADDRESS;
        int connectionTimeout = 5000;

        client = CuratorFrameworkFactory.newClient(Constant.ZK_ADDRESS, new RetryNTimes(3, connectionTimeout));
        client.start();
        watcherPath(Constant.ZK_PATH);
    }

    /**
     * 函数入口
     *
     * @param args
     */
    public static void main(String[] args) {
        AppMonitor bootStrap = new AppMonitor();
        try {
            bootStrap.initialize();
            Thread.sleep(100000000);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 对path进行监听配置
    public void watcherPath(String path) throws Exception {
        //子节点的监听
        //PathChildrenCache cache = new PathChildrenCache(client, path, false);
        //cache.start();
        //cache.getListenable().addListener(plis);

        //对path路径下所有孩子节点的监听
        TreeCache treeCache = new TreeCache(client, path);
        treeCache.start();
        treeCache.getListenable().addListener(treeCacheListener);
    }

    /**
     * 所有子节点的监听
     */
    TreeCacheListener treeCacheListener = new TreeCacheListener() {

        public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
            switch (event.getType()) {
                case NODE_ADDED:
                    System.out.println("TreeNode added: " + getTime() + " " + event.getData().getPath() + " , data: " + new String(event.getData().getData()));
                    break;
                case NODE_UPDATED:
                    System.out.println("TreeNode updated: " + getTime() + " " + event.getData().getPath() + " , data: " + new String(event.getData().getData()));
                    break;
                case NODE_REMOVED:
                    System.out.println("TreeNode removed: " + getTime() + " " + event.getData().getPath());
                    break;
                default:
                    break;
            }

            //TreeCache treeCache = new TreeCache(client, Constant.ZK_PATH);
            //treeCache.start();
            //treeCache.getListenable().addListener(treeCacheListener);
        }
    };

    public static String getTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    /**
     * 子节点的监听
     */
    static PathChildrenCacheListener plis = new PathChildrenCacheListener() {

        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            switch (event.getType()) {
                case CHILD_ADDED: {
                    System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                }

                case CHILD_UPDATED: {
                    System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                }

                case CHILD_REMOVED: {
                    System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                }
            }

        }
    };

}
